package com.study.thirdlib.livedatabus

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.jeremyliao.liveeventbus.LiveEventBus
import com.study.thirdlib.livedatabus.event.MyMsg
import com.study.thirdlib.R
import com.study.thirdlib.databinding.ActivityBusaBinding
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity

/**
 * <pre>
 *     author : slx
 *     time   : 2020/11/25
 *     desc   :
 * </pre>
 */
class BusAActivity : AppCompatActivity() {

    lateinit var mBinding: ActivityBusaBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_busa)

        // 订阅消息
        // 生命周期感知方式
        LiveEventBus.get("key", String::class.java)
            .observe(this, {
                mBinding.tvAlabel.text = it
            })

        // 订阅自定义消息
        LiveEventBus.get(MyMsg::class.java)
            .observe(this, {
                mBinding.tvAlabel2.text = it.msg
            })


        mBinding.btnStart.onClick {
            startActivity<BusBActivity>()
        }
        // Forever方式订阅,需要手动取消订阅
        /*LiveEventBus.get("key",String::class.java.javaClass)
            .observeForever {  }*/
    }
}